package com.sxj.corejava.code02_jdbc;

import com.sxj.corejava.entity.User;

import java.sql.*;

/**
 * @author 石小俊
 * @date 2023年12月2023/12/2日 15:36
 */
public class Test11_元数据 {

    public static void main(String[] args) {
//        selectAll();
        User user = new User("aaa","123","13912345678","江苏-南京");
        int id = insertReturnPrimaryKey(user);
        System.out.println("id:"+id);
    }

    public static void selectAll() {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdbc?useUnicode=true&characterEncoding=utf-8", "root", "");

            // 根据连接获取数据库的元数据
            // 数据库元数据可以获取到与数据库相关的信息
            DatabaseMetaData data = conn.getMetaData();
            System.out.println("数据库产品名:" + data.getDatabaseProductName());
            System.out.println("数据库版本号:"+data.getDatabaseProductVersion());
            System.out.println("数据库驱动:"+data.getDriverName());
            System.out.println("驱动版本:"+data.getDriverVersion());
            System.out.println("数据库用户:"+data.getUserName());
            System.out.println("URL地址:"+data.getURL());

            String sql = new StringBuffer()
                    .append(" select id,username,password,phone,address ")
                    .append(" from t_user")
                    .toString();
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            ResultSetMetaData rsmd = rs.getMetaData();
            int count = rsmd.getColumnCount();
            System.out.println("结果集中列的数量:"+count);
            for(int i = 1; i <= count; i++){
                System.out.println("第"+i+"列的类型:"+rsmd.getColumnTypeName(i));
                System.out.println("第"+i+"列的列名:"+rsmd.getColumnName(i));
                System.out.println("第"+i+"列所在的表:"+rsmd.getTableName(i));
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.out.println("未找到驱动");
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("连接数据库失败");
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                    System.out.println("释放连接出错");
                }
            }
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static Integer insertReturnPrimaryKey(User user) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        Integer id = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdbc?useUnicode=true&characterEncoding=utf-8", "root", "");
            String sql = new StringBuffer()
                    .append(" insert into ")
                    .append(" t_user ")
                    .append("   (username,password,phone,address)")
                    .append(" values ")
                    .append("   (?,?,?,?)")
                    .toString();
            ps = conn.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
            ps.setString(1, user.getUsername());
            ps.setString(2, user.getPassword());
            ps.setString(3, user.getPhone());
            ps.setString(4, user.getAddress());
            ps.executeUpdate();
            rs = ps.getGeneratedKeys();
            ResultSetMetaData rsmd = rs.getMetaData();
            int count = rsmd.getColumnCount();
            System.out.println("结果集中列的数量:"+count);
            for(int i = 1; i <= count; i++){
                System.out.println("第"+i+"列的类型:"+rsmd.getColumnTypeName(i));
                System.out.println("第"+i+"列的列名:"+rsmd.getColumnName(i));
                System.out.println("第"+i+"列所在的表:"+rsmd.getTableName(i));
            }
            while (rs.next()) {
//                id = rs.getInt(1);
                id = rs.getInt("GENERATED_KEY");
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.out.println("未找到驱动");
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("连接数据库失败");
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                    System.out.println("释放连接出错");
                }
            }
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return id;
    }
}
